package org.jiuling.wecom.school;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.verify.Assertions;
import org.jiuling.local.tools.verify.ObjectVerify;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.enums.RequestMethodEnum;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.wecom.basics.Authorization;
import org.jiuling.wecom.config.WeComConfig;
import org.jiuling.wecom.school.enums.WeComSchoolApiEnum;
import org.jiuling.wecom.school.model.StudentModel;
import org.jiuling.wecom.school.verify.RequestVerify;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生家长管理
 * @author 紫灵
 * @date 2023年04月03日 16时36分33秒
 */
public class StudentParentManage {

    /** 授权 */
    private final Authorization authorization;

    public StudentParentManage(Authorization authorization) {
        this.authorization = authorization;
    }

    /**
     * 获取部门学生列表
     * @param departmentId 部门id
     */
    public List<StudentModel> getDivisionalStudentList(Long departmentId){
        Assertions.longIsNotBlank("部门id不能为空！",departmentId);
        HttpRequestParam httpRequestParam = new HttpRequestParam()
                .add("access_token",authorization.getToken().getAccessToken())
                .add("department_id",departmentId);
        HttpRequest httpRequest = new HttpRequest();
        try {
            HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(WeComSchoolApiEnum.GET_DIVISIONAL_STUDENT_LIST.getApi());
            httpRequestFactory.setRequestParam(httpRequestParam);
            httpRequestFactory.setRequestMethod(RequestMethodEnum.GET);
            JSONObject jsonObject = JSON.parseObject(httpRequest.send().getBody());
            RequestVerify.errorVerify(jsonObject);
            // 获取学生
            List<StudentModel> studentModelList = new ArrayList<>();
            JSONArray studentJsonArray = jsonObject.getJSONArray("students");
            if(!ObjectVerify.isNull(studentJsonArray) && !studentJsonArray.isEmpty()) {
                for (int i = 0; i < studentJsonArray.size(); i++) {
                    studentModelList.add(StudentModel.builder().addStudentByJson(studentJsonArray.getJSONObject(i)).build());
                }
            }
            return studentModelList;
        } catch (RequestException | IOException | LocalToolsException | NoSuchAlgorithmException |
                 KeyManagementException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        WeComConfig weComConfig = WeComConfig.builder()
                .addAgentId("1000003")
                .addCorpId("wwb8f187e1b4973254")
                .addCorpSecret("4Y-oi0dE8pRKUVjtoWI-Z3oOvv-9in2RPkdZ2S-sDA8")
                .build();
        Authorization authorization = new Authorization(weComConfig);
        StudentParentManage studentParentManage = new StudentParentManage(authorization);
        List<StudentModel> divisionalStudentList = studentParentManage.getDivisionalStudentList(102L);
        divisionalStudentList.forEach(studentModel -> {
            studentModel.getParentList().forEach(parentModel -> {
                System.out.println(parentModel.getRelation());
            });
            System.out.println(studentModel.getName());
        });
    }

}
